 $(document).ready(function() {
             //		权限判断
		if($.cookie('ID') == null){
			window.location.href="http://www.duoweish.com/crm/login.html";
		}
//		2，3号权限
		if($.cookie('Right') == 1){
			window.location.href="http://www.duoweish.com/crm/index.html";
		}
 	$('.dw_name').text($.cookie('Name'));
		 $('.dw_part').text($.cookie('Department'));
			  var page_num = 0;		
             $(".select2").select2();
            $('#reservation').daterangepicker({
             	format:'YYYY-MM-DD',
             	startDate:'2017-10-10',
             	locale:{
             		format:'YYYY-MM-DD'
             	}
             });
             
             init(1);
             $('.search_btn').click(function(){
             	 
			  Ajax(1)
				
            })
            
            function Ajax(curr){
            	 var Kind_data = 0;
			      var onoff = "True";//客户名or项目名
			      var sea_text =$.trim($('.search_text').val());//搜索框内容
				  var data_rang = $('.time_range').val();//时间范围
				  
				  var start = data_rang.split(' -')[0];
				  var end = data_rang.split('- ')[1];
				if(data_rang==""){onoff="False";}
				if(curr==1){
					onoff = "False";
				}
            	$.ajax({
				headers:{
				    	Accept:"application/json;charset=utf-8"
				    },
					type:"get",
					url:"http://www.duoweish.com/apis/ProjectStock/",
					data:{
                            UserId:$.cookie('ID'),
							Date:onoff,
							DateBegin:start,
							DateEnd:end,
							Content:sea_text,
							page:curr||1,
							rows:8
					},
					success:function(data){
						console.log(curr)
						if(data.length!=0){
							page_num = data[1];
						    tableData(data);
					     	divePage(page_num,curr);
						}else{
							layer.msg('没找到该商家')
						}
						
						}
				})
            	
            }
            function tableData(ret){
               
                    $('#table_body').empty();
                    for(var i = 0;i < ret.length;i++){
                    	for (var j = 0;j<ret[i].length;j++) {
                    		
                    	
                        var tr = `<tr>
                                <td style="line-height:20px;">${ret[i][j][0]}</td>
                                <td style="line-height:20px;">${ret[i][j][1]}</td>
                                <td style="line-height:20px;">${ret[i][j][2]}</td>
                                <td style="line-height:20px;">${ret[i][j][3]}</td>
                                <td index="${ret[i][j][4]}">
                                   <button  class="look_ok">确认</button>
                                </td>
                               
                            </tr>`;
                        $('#table_body').append(tr);
                    }
                }
                   
                   
                   
                   
                 $('.look_ok').click(function(){
                 	console.log($(this).parent().prev().text())
                 	      var index = $(this).parent().attr("index");
                 	     if($(this).parent().prev().text()=='断货中'){
							$.ajax({
								type:"get",
								url:"http://www.duoweish.com/apis/NewSupply/",
								data:{
									Id:index,
									UserId:$.cookie('ID')
								},
								success:function(){
	  								layer.msg('确认成功');	
								}
							})
						}else{
	  						layer.alert('请等待审核通过',{skin:"demo-class"}); 
						}		
                 })
                 /////////////
                 
      
                    
            }
            $('.add_new').click(function(){
            	     layer.open({
								        type: 2
								        ,title: '添加断货'
								        ,closeBtn: 1
								        ,area:['420px','300px']
								        ,shadeClose:true
								        ,shade: 0.8
								        ,id: 'LAY_layuipro' 
								       ,anim:1
								        ,btnAlign: 'c'
								        ,moveType: 1
								        ,content:'http://www.duoweish.com/crm/new_break.html'
									    ,success: function(){
									    	
						          	}
						    	 })
            })
           function divePage(page_num,curr){
            layui.use(['laypage', 'layer'], function(){
					 var laypage = layui.laypage
			  		,layer = layui.layer;
			  	
                     laypage.render({
						    elem: 'demo0'
						    ,count: page_num
						    ,limit:8
						    ,curr:curr
						    ,jump: function(obj, first){
						    	   if(!first){
						                Ajax(obj.curr);
						               }
				        
						      		}
						   		});
					 })		
           }
          
           function init(curr){ 
           	     Ajax(1)
	            divePage(page_num);
           }
          
     
           
})